﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Evolution.Common;
using RamaSoft.Finanças;
using ComponentFactory.Krypton.Toolkit;

namespace TECassist
{
    public partial class DespLçt : KryptonForm
    {
        public static Int32 códDesp;
        Database myDBase;
        double rotValor;
        DateTime rotVençto;
        int rotQuant, rotNúmero, rotSeque;
        string rotTipo;

        public DespLçt(Database myDBase)
        {
            InitializeComponent();
            this.myDBase = myDBase;
        }

        private void Form_Load(object sender, EventArgs e)
        {
            DespesaBase regDesp = new DespesaBase(myDBase);
            regDesp.Código = códDesp;
            int dbRet = regDesp.Obter();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft\n"
                    + "referência: " + this.Name + "Load\n" + regDesp.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            titDespesa.Text = regDesp.Descrição.Trim();
        }

        private void cmdVoltar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void opçTipo_Click(object sender, EventArgs e)
        {
            int qOpç = Convert.ToInt32(((RadioButton)sender).Tag);
            if (qOpç == 1)
            {
                janConstante.Enabled = true;
                janParcelas.Enabled = false; 
                janÚnico.Enabled = false;
                txConst.Focus(); 
            }
            else if (qOpç == 2)
            {
                janConstante.Enabled = false;
                janParcelas.Enabled = true;
                janÚnico.Enabled = false;
                nmParcelas.Focus(); 
            }
            else
            {
                janConstante.Enabled = false;
                janParcelas.Enabled = false;
                janÚnico.Enabled = true;
                txÚnico.Focus();
            }
        }

        private void txRefer_TextChanged(object sender, EventArgs e)
        {
            if (txRefer.Text.Length > 3)
            {
                dataInício.Enabled = true;
                dataVençto.Enabled = true;
                janTipo.Enabled = true; 
            }
            else
            {
                dataInício.Enabled = false;
                dataVençto.Enabled = false;
                janTipo.Enabled = false;
                janConstante.Enabled = false;
                janParcelas.Enabled = false;
                janÚnico.Enabled = false;
            }
        }

        private void txRefer_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter) dataInício.Focus();
        }

        private void txConst_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                cmdConstOK.Enabled = true;
                cmdConstOK.Focus();
                return;
            }
            if (e.KeyChar == ',' && !txConst.Text.Contains(",")) return;
            if (e.KeyChar != (char)Keys.Back &&
                (e.KeyChar < '0' || e.KeyChar > '9')) e.Handled = true;
        }

        private void txÚnico_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                cmdÚnicoOK.Enabled = true;
                cmdÚnicoOK.Focus();
                return;
            }
            if (e.KeyChar == ',' && !txÚnico.Text.Contains(",")) return;
            if (e.KeyChar != (char)Keys.Back &&
                (e.KeyChar < '0' || e.KeyChar > '9')) e.Handled = true;
        }

        private void nmParcelas_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter) txPrima.Focus();
        }

        private void txPrima_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter) txDemais.Focus();
            if (e.KeyChar == ',' && !txPrima.Text.Contains(",")) return;
            if (e.KeyChar != (char)Keys.Back &&
                (e.KeyChar < '0' || e.KeyChar > '9')) e.Handled = true;
        }

        private void txDemais_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (txDemais.Text.Length > 0) cmdParceOK.Enabled = true;
                txÚltima.Focus();
                return;
            }
            if (e.KeyChar == ',' && !txDemais.Text.Contains(",")) return;
            if (e.KeyChar != (char)Keys.Back &&
                (e.KeyChar < '0' || e.KeyChar > '9')) e.Handled = true;
        }

        private void txÚltima_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                if (txDemais.Text.Length > 0) cmdParceOK.Focus();
                else txDemais.Focus();
                return;
            }
            if (e.KeyChar == ',' && !txÚltima.Text.Contains(",")) return;
            if (e.KeyChar != (char)Keys.Back &&
                (e.KeyChar < '0' || e.KeyChar > '9')) e.Handled = true;
        }

        private void dataInício_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter) dataVençto.Focus();
        }

        private void dataVençto_Enter(object sender, EventArgs e)
        {
            dataVençto.MinDate = dataInício.Value;
        }

        private void dataVençto_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter) janTipo.Focus();
        }

        private void cmdConstOK_Click(object sender, EventArgs e)
        {
            myDBase.BeginTransaction(IsolationLevel.ReadCommitted);
            rotValor = Convert.ToDouble(txConst.Text);
            rotTipo = "K";
            rotQuant = 0;
            int rotRet = rot_Grava();
            if (rotRet != 0)
            {
                myDBase.RollBackTransaction();
                return;
            }
            rotVençto = dataVençto.Value;
            DateTime datada = Convert.ToDateTime(DateTime .Today.ToString ("01/MM/yyyy"));
            datada = datada.AddMonths(1);
            rotSeque = 1;
            while (rotVençto < datada)
            {
                rotRet = rot_Parcela();
                if (rotRet != 0)
                {
                    myDBase.RollBackTransaction();
                    return;
                }
                rotSeque = rotSeque + 1;
                rotVençto = rotVençto.AddMonths(1);
            }
            myDBase.CommitTransaction();
            infoOK.Visible = true;
            Pulso.Enabled = true;
        }

        private int rot_Grava()
        {
            DespesaRefer regRefer = new DespesaRefer(myDBase);
            regRefer.Código = códDesp;
            regRefer.Tipo = rotTipo;
            regRefer.Descrição = txRefer.Text;
            regRefer.Parcelas = rotQuant;
            regRefer.Valor = rotValor;
            regRefer.DiaVençto = dataVençto.Value.Day;  
            int dbRet = regRefer.Insert();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft\n"
                    + "referência: " + this.Name + "GravRef\n" + regRefer.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return dbRet ;
            }
            dbRet = regRefer.novoNúmero();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft\n"
                    + "referência: " + this.Name + "GravNumo\n" + regRefer.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return dbRet;
            }
            rotNúmero = regRefer.Número;
            return 0;
        }

        private int rot_Parcela()
        {
            Despesa regDesp = new Despesa(myDBase);
            regDesp.Número = rotNúmero;
            regDesp.Parcelas = rotSeque;
            regDesp.Vencimento = rotVençto;
            regDesp.Valor = rotValor;
            regDesp.Pagamento = Convert.ToDateTime("1/1/1901");
            regDesp.ValPago = 0;
            int dbRet = regDesp.Insert();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft\n"
                    + "referência: " + this.Name + "GravDesp\n" + regDesp.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return dbRet;
            }
            return 0;
        }

        private void cmdÚnicoOK_Click(object sender, EventArgs e)
        {
            myDBase.BeginTransaction(IsolationLevel.ReadCommitted);
            rotValor = Convert.ToDouble(txÚnico.Text);
            rotTipo = "U";
            rotQuant = 1;
            int rotRet = rot_Grava();
            if (rotRet != 0)
            {
                myDBase.RollBackTransaction();
                return;
            }
            rotVençto = dataVençto.Value;
            rotSeque = 1;
            rotRet = rot_Parcela();
            if (rotRet != 0)
            {
                myDBase.RollBackTransaction();
                return;
            }
            myDBase.CommitTransaction();
            infoOK.Visible = true;
            Pulso.Enabled = true;
        }

        private void cmdParceOK_Click(object sender, EventArgs e)
        {            
            myDBase.BeginTransaction(IsolationLevel.ReadCommitted);
            rotValor = Convert.ToDouble(txDemais.Text);
            rotTipo = "P";
            rotQuant = (int)nmParcelas.Value; 
            int rotRet = rot_Grava();
            if (rotRet != 0)
            {
                myDBase.RollBackTransaction();
                return;
            }
            rotVençto = dataVençto.Value;
            rotSeque = 1;
            if (txPrima.Text.Length > 0)
            {
                rotValor = Convert.ToDouble(txPrima.Text);
                rotRet = rot_Parcela();
                if (rotRet != 0)
                {
                    myDBase.RollBackTransaction();
                    return;
                }
                rotSeque = rotSeque + 1;
                rotVençto = rotVençto.AddMonths(1);
            }
            rotValor = Convert.ToDouble(txDemais.Text);
            if (txÚltima.Text.Length > 0) rotQuant = rotQuant - 1;
            while (rotQuant >= rotSeque)
            {
                rotRet = rot_Parcela();
                if (rotRet != 0)
                {
                    myDBase.RollBackTransaction();
                    return;
                }
                rotSeque = rotSeque + 1;
                rotVençto = rotVençto.AddMonths(1);
            }
            if (txÚltima.Text.Length > 0)
            {
                rotValor = Convert.ToDouble(txÚltima.Text);
                rotRet = rot_Parcela();
                if (rotRet != 0)
                {
                    myDBase.RollBackTransaction();
                    return;
                }
            }
            myDBase.CommitTransaction();
            infoOK.Visible = true;
            Pulso.Enabled = true;
        }

        private void Pulso_Tick(object sender, EventArgs e)
        {
            Pulso.Enabled = false;
            infoOK.Visible = false;
            this.Close();
        }

        private void nmParcelas_ValueChanged(object sender, EventArgs e)
        {
            if (nmParcelas.Value > 1)
            {
                txPrima.Enabled = true;
                txDemais.Enabled = true;
                txÚltima.Enabled = true;
                if (txDemais.Text.Length > 0) cmdParceOK.Enabled = true;
                txPrima.Focus();
            }
            else
            {
                txPrima.Enabled = false;
                txDemais.Enabled = false;
                txÚltima.Enabled = false;
                cmdParceOK.Enabled = false;
            }
        }
    }
}
